package com.hanyizhu.demostore.config;

import com.hanyizhu.demostore.interceptor.LoginInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

/**处理器拦截器的注册
 * @author lenovo*/
@Configuration //去加载当前的拦截器并进行注册
public class LoginInterceptorConfigurer implements WebMvcConfigurer {
    /**
     * 用这个方法注册拦截器的注册
     * 配置拦截器
     *  default void addInterceptors(InterceptorRegistry registry) {
     *     }
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //1.创建自定义拦截器对象
        HandlerInterceptor interceptor = new LoginInterceptor();
        //3.配置白名单:存放在一个List集合
        List<String> patterns = new ArrayList<>();
        patterns.add("/bootstrap3/**");
        patterns.add("/css/**");
        patterns.add("/images/**");
        patterns.add("/js/**");
        patterns.add("/web/register.html");
        patterns.add("/web/login.html");
        patterns.add("/web/index.html");
        patterns.add("/web/product.html");
        patterns.add("/users/reg");
        patterns.add("/users/login");
        patterns.add("/districts/**");
        patterns.add("/products/**");

        //2.完成拦截器的注册
        registry.addInterceptor(interceptor)
                //拦截
                .addPathPatterns("/**")
                //开启白名单
                .excludePathPatterns(patterns);


    }
}
